# -*- encoding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see http://www.gnu.org/licenses/.
#
##############################################################################

from openerp import models, api, fields
import openerp.addons.decimal_precision as dp


class qikuai_line(models.Model):
    _name= 'account.order.line'


    @api.one
    @api.depends('price', 'product_qty')
    def _compute_amount(self):
        if self.product_qty and self.price:
            self.amount = self.product_qty * self.price

    @api.one
    @api.depends('product_tpl_id',
                 'product_tpl_id.attribute_line_ids')
    def _get_possible_attribute_values(self):
        attr_values = self.env['product.attribute.value']
        # 只有一个属性时选第一个属性
        index = (len(self.product_tpl_id.attribute_line_ids) <2) and 1 or 0
        for attr_line in self.product_tpl_id.attribute_line_ids:
            if self.product_tpl_id.name==u'板材':
                if index==2:
                    attr_values |= attr_line.value_ids
            elif index == 1:
                attr_values |= attr_line.value_ids
            index +=1
        self.possible_values = attr_values.sorted()

   #定义代理合同需要的规格属性
    @api.one
    @api.depends('product_tpl_id',
                 'product_tpl_id.attribute_line_ids')
    def _get_possible_attribute_values_spec(self):
        attr_values = self.env['product.attribute.value']
        # 只有一个属性时选第一个属性
        index = (len(self.product_tpl_id.attribute_line_ids) <2) and 1 or 0
        for attr_line in self.product_tpl_id.attribute_line_ids:
            if self.product_tpl_id.name==u'板材':
                if index==5:
                    attr_values |= attr_line.value_ids
            elif index == 0:
                attr_values |= attr_line.value_ids
            index +=1

        self.possible_spec_values = attr_values.sorted()

    @api.one
    @api.onchange('product_tpl_id')
    def onchange_product_tpl_id(self):
        self._get_possible_attribute_values()
        self._get_possible_attribute_values_spec()


    product_tpl_id = fields.Many2one(comodel_name='product.template',
                                       string=u'产品名称', domain="[('sale_ok','=',1)]")

    possible_values = fields.Many2many(
        comodel_name='product.attribute.value',
        compute='_get_possible_attribute_values')
    product_level = fields.Many2one(comodel_name='product.attribute.value', string=u'产品级别规格型号', domain="[('id', 'in', possible_values[0][2])]")
    
    #代理合同所需要的字段
    possible_spec_values = fields.Many2many(
        comodel_name='product.attribute.value',
        compute='_get_possible_attribute_values_spec')
    product_spec = fields.Many2one(comodel_name='product.attribute.value', string=u'包装', domain="[('id', 'in', possible_spec_values[0][2])]")
    
    product_uom = fields.Many2one(
        comodel_name='product.uom', string=u'单位',
        related='product_tpl_id.uom_id')

    #代理合同所需要的字段
    min_qty=fields.Float(string=u"最低销售数量",digits=dp.get_precision('Product Unit of Measure'))

    product_qty = fields.Float(
        u'数量', digits=dp.get_precision('Product Unit of Measure'))
    price = fields.Float(u'单价(元)', required=True)

    amount = fields.Float(
        compute=_compute_amount, string=u'金额',
        digits_compute=dp.get_precision('Account'))



    account_bc_id=fields.Many2one('account.analytic.account',string=u"合同")
    account_qk_id=fields.Many2one('account.analytic.account',string=u"合同")

